package com.navidove.web.manager.ctrl;


import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.navidove.appspotlight.model.bo.SysUser;
import com.navidove.appspotlight.model.service.SysUserService;
import com.navidove.web.vo.DisplaytagPaging;

@Controller
@RequestMapping("/system/sysUser")
@SessionAttributes(value={"sysUserQueryCtrl.query","sysUserQueryCtrl.paging"})
public class SysUserQueryCtrl {
	
	@Autowired
	private SysUserService sysUserService;
	
	public class UserQuery implements Serializable {
		private static final long serialVersionUID = 351050505286016828L;
		private String username;
		private String email;
		public String getUsername() {
			return username;
		}
		public void setUsername(String username) {
			this.username = username;
		}
		public String getEmail() {
			return email;
		}
		public void setEmail(String email) {
			this.email = email;
		}
		
	}
    
    @ModelAttribute("sysUserQueryCtrl.paging")
    public DisplaytagPaging createDisplaytagPaging() {
        return new DisplaytagPaging();
    }
    
    @ModelAttribute("sysUserQueryCtrl.query")
    public UserQuery createQuery() {
        return new UserQuery();
    }
    
    @ModelAttribute
    public void reset(Model model) {        
        model.addAttribute("query", createQuery());
        model.addAttribute("paging", createDisplaytagPaging()); 
    }

    @RequestMapping("/init")
    public String init(Model model) {
        return query(model, createQuery(), createDisplaytagPaging());        
    }    
    
    @RequestMapping("/query")
    public String query(Model model, 
            @ModelAttribute("query") UserQuery query,
            @ModelAttribute("paging") DisplaytagPaging paging) {
        
        if (StringUtils.isBlank(paging.getSort())) {
            paging.setSort("username");
            paging.setAsc(true);
        }
        
        List<SysUser> list = sysUserService.getSysUsersByConditions(query.getUsername(), query.getEmail(), paging);
        model.addAttribute("sysUserList", paging.createPaginatedList(list));
                
        model.addAttribute("sysUserQueryCtrl.query", query);
        model.addAttribute("sysUserQueryCtrl.paging", paging);
        model.addAttribute("query", query);
        model.addAttribute("paging", paging);        
        return "/system/sysUser_list";
    }    
    
    @RequestMapping("/back")
    public String back(Model model,
            @ModelAttribute("sysUserQueryCtrl.query") UserQuery query,
            @ModelAttribute("sysUserQueryCtrl.paging") DisplaytagPaging paging) {
        
        query = query  != null ? query : createQuery();
        paging = paging != null ? paging : createDisplaytagPaging();
                
        return this.query(model, query, paging);
    }    
}
